/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import type.RawMaterialPurchaseOrderStatus;
import type.VendorInvoiceStatus;

/**
 *
 * @author wei
 */
@Entity(name = "RawMaterialPurchaseOrder")
public class RawMaterialPurchaseOrderEntity extends DataObjectEntity implements Serializable {

    private double star;

    public double getStar() {
        return star;
    }

    public void setStar(double star) {
        this.star = star;
    }
    @OneToOne
    private RawMaterialQuotationEntity rmq;
    //list of settle shipment .Same value as scheduleShip.Removing key when invoice is created.
    private HashMap settleShip = new HashMap();

    public HashMap getSettleShip() {
        return settleShip;
    }

    public void setSettleShip(HashMap settleShip) {
        this.settleShip = settleShip;
    }
    private HashMap scheduleShip = new HashMap();

    public HashMap getScheduleShip() {
        return scheduleShip;
    }

    public void setScheduleShip(HashMap Scheduleship) {
        this.scheduleShip = Scheduleship;
    }
    private HashMap ship = new HashMap();

    public HashMap getShip() {
        return ship;
    }
    private double amountLeft;

    public double getAmountLeft() {
        return amountLeft;
    }

    public void setAmountLeft(double amountLeft) {
        this.amountLeft = amountLeft;
    }

    public void setShipAmount(HashMap shipAmount) {
        this.ship = shipAmount;
    }
    private String note;
    private boolean updated = false;

    public boolean isUpdated() {
        return updated;
    }

    public void setUpdated(boolean updated) {
        this.updated = updated;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public RawMaterialQuotationEntity getRmq() {
        return rmq;
    }

    public void setRmq(RawMaterialQuotationEntity rmq) {
        this.rmq = rmq;
    }
    private RawMaterialPurchaseOrderStatus status;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateConfirmed;
    @OneToMany(mappedBy = "rawMaterialPurchaseOrder")
    private Collection<VendorInvoiceEntity> vendorInvoices = new ArrayList<VendorInvoiceEntity>();
    private String bankAccount;

    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    public RawMaterialPurchaseOrderEntity() {
        super();
    }

    public Collection<VendorInvoiceEntity> getVendorInvoices() {
        return vendorInvoices;
    }

    public void setVendorInvoices(Collection<VendorInvoiceEntity> vendorInvoices) {
        this.vendorInvoices = vendorInvoices;
    }

    public Date getDateConfirmed() {
        return dateConfirmed;
    }

    public void setDateConfirmed(Date dateConfirmed) {
        this.dateConfirmed = dateConfirmed;
    }

//    public Collection<ProductQuantityPriceEntity> getProductQuantityPriceList() {
//        return productQuantityPriceList;
//    }
//
//    public void setProductQuantityPriceList(Collection<ProductQuantityPriceEntity> productQuantityPriceList) {
//        this.productQuantityPriceList = productQuantityPriceList;
//    }
    public RawMaterialPurchaseOrderStatus getStatus() {
        return status;
    }

    public void setStatus(RawMaterialPurchaseOrderStatus status) {
        this.status = status;
    }
    private double price;
    private double cost;
    private double quantity;
    private double figureCost;

    public double getFigureCost() {
        return quantity * price;
    }

    public void setFigureCost(double figureCost) {
        this.figureCost = figureCost;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getQuantity() {
        return quantity;
    }

    public void setQuantity(double quantity) {
        this.quantity = quantity;
    }
//    public VendorEntity getVendor() {
//        return vendor;
//    }
//
//    public void setVendor(VendorEntity vendor) {
//        this.vendor = vendor;
//    }

    @Override
    public String toString() {
        return "Purchase Order # " + this.getId();
    }

    @Override
    public String childToString() {
        return "RawMaterialPurchaseOrderEntity{" + "star=" + star + ", rmq=" + rmq + ", ship=" + ship + ", amountLeft=" + amountLeft + ", status=" + status + ", dateConfirmed=" + dateConfirmed + ", vendorInvoices=" + vendorInvoices + '}';
    }

    public boolean isFulfilled() {
        if (this == null) {
            return false;
        }
        return this.getStatus().equals(RawMaterialPurchaseOrderStatus.fulfilled);
    }
    private boolean due;

    public boolean isDue() {
        if(this ==null)return false;
        Collection<VendorInvoiceEntity> list = this.getVendorInvoices();
        if(list.size()==0)        return false;
        for (VendorInvoiceEntity ve : list) {
            if(ve.getStatus().equals(VendorInvoiceStatus.settled))continue;
            Date d = ve.getDueDate();
            if(d==null)return false;
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, 7);
            
            Date d1 = calendar.getTime();
            System.out.println(d1.toString()+"uuuu");
            if (d1.after(d)) {
                return true;
            }
        }
        return false;
    }

    public void setDue(boolean due) {
        this.due = due;
    }
}
